package com.weiqiao.mall.rest;

import com.mingsoft.util.StringUtil;
import com.weiqiao.aop.log.Log;
import com.weiqiao.utils.EndUtils;
import com.weiqiao.utils.StringUtils;
import com.weiqiao.utils.WeiXinOssUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@RestController
@RequestMapping("/oss")
public class OSSUploadController {

    // 压缩过的图片
    @Log("上传,压缩")
    @PostMapping("/user-head-upload")
    public ResponseEntity userHeadUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String company_id = request.getParameter("company_id");
        if (StringUtils.isBlank(company_id)) {
            company_id = "122";
        }
        String folder = request.getParameter("folder");
        // 上传文件分目录
        if (StringUtils.isBlank(folder)) {
            folder = "mall";
        }
        InputStream inputStream = null;
        boolean flag = false;
        String path = "";
        if (!StringUtil.isBlank(file)) {
            // 获取图片名字
            String fileName = file.getOriginalFilename();
            // 获取图片后缀
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            // 拼接路径
            try {
                byte[] bytes = file.getBytes();
                String name = EndUtils.getByteMD5(bytes);
                path = "mall/" + company_id + "/"  + name + suffixName;
                inputStream = file.getInputStream();
                // 上传图片至OSS服务器
                flag = WeiXinOssUtil.importInputStream(inputStream, path, file);

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeQuietly(inputStream);
            }
        }
        if (flag) {
            return new ResponseEntity(WeiXinOssUtil.iconHttp + path, HttpStatus.OK);
        } else {
            return new ResponseEntity(false, HttpStatus.OK);
        }
    }

    @Log("上传,无压缩")
    @PostMapping("/album-Upload")
    public ResponseEntity albumUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String company_id = request.getParameter("company_id");
        String folder = request.getParameter("folder");
        // 上传文件分公司id
        if (StringUtils.isBlank(company_id)) {
            company_id = "122";
        }
        // 上传文件分目录
        if (StringUtils.isBlank(folder)) {
            folder = "mall";
        }
        InputStream inputStream = null;
        boolean flag = false;
        String path = "";
        if (!StringUtil.isBlank(file)) {
            // 获取图片名字
            String fileName = file.getOriginalFilename();
            // 获取图片后缀
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            // 拼接路径
            try {
                byte[] bytes = file.getBytes();
                String name = EndUtils.getByteMD5(bytes);
                path = folder + "/" + company_id + "/" + name + suffixName;
                inputStream = file.getInputStream();
                // 上传图片至OSS服务器
                flag = WeiXinOssUtil.importInputStream(inputStream, path);

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeQuietly(inputStream);
            }
        }
        if (flag) {
            return new ResponseEntity(WeiXinOssUtil.iconHttp + path, HttpStatus.OK);
        } else {
            return new ResponseEntity(false, HttpStatus.OK);
        }
    }

    /**
     * 多文件上传，上传图片无压缩
     *
     * @param files
     * @return
     */
    @PostMapping("/multi-file-Upload")
    public ResponseEntity multifileUpload(@RequestParam("files") MultipartFile[] files, HttpServletRequest request) {
        String company_id = request.getParameter("company_id");
        List<String> list = new ArrayList<>();
        String folder = request.getParameter("folder");
        // 上传文件分公司id
        if (StringUtils.isBlank(company_id)) {
            company_id = "122";
        }
        // 上传文件分目录
        if (StringUtils.isBlank(folder)) {
            folder = "mall";
        }
        InputStream inputStream = null;
        try {
            if (!StringUtil.isBlank(files)) {
                for (MultipartFile f : files) {
                    String path;
                    // 获取图片名字
                    String fileName = f.getOriginalFilename();
                    // 获取图片后缀
                    String suffixName = fileName.substring(fileName.lastIndexOf("."));
                    // 拼接路径
                    byte[] bytes = f.getBytes();
                    String name = EndUtils.getByteMD5(bytes);
                    path = folder + "/" + company_id + "/" + name + suffixName;
                    inputStream = f.getInputStream();
                    // 上传图片至OSS服务器
                    WeiXinOssUtil.importInputStream(inputStream, path);
                    list.add(WeiXinOssUtil.iconHttp + path);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != inputStream) {
                IOUtils.closeQuietly(inputStream);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("errno", 0);
        map.put("id", "" + new Date().getTime());
        map.put("data", list);
        return new ResponseEntity(map,HttpStatus.OK);
    }
}
